package com.googlecode.continuity.core.server.mvc.controller;

import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import com.googlecode.continuity.core.client.ContinuityUser;
import com.googlecode.continuity.core.client.User;
import com.googlecode.continuity.core.server.mvc.ContinuityModel;
import com.googlecode.continuity.core.server.mvc.command.EditUserCommand;
import com.googlecode.continuity.core.server.security.SecurityUserToken;

/**
 * RegistrationFormController serves a page that allows for a new user to register.  Provided that all data is valid,
 * the controller creates a new user in the database.
 *
 * @author Peter Schmitt <aragos@aragos.de>
 */
public class RegistrationFormController extends EditUserFormController {

  /**
   * Handles a successful submit request and registers the new users with the database.
   *
   * @param obj command object
   * @return success view
   * @throws ServletException
   */
  public ModelAndView onSubmit(Object obj) throws ServletException {
    EditUserCommand command = (EditUserCommand) obj;
    SecurityUserToken userToken = new SecurityUserToken();
    ContinuityUser user = command.getUser();
    userToken.setUser(user);
    userToken.setPassword(getUserService().encodePassword(command.getNewpass1()));
    getUserService().setInitialValues(userToken);
    getUserService().storeUser(userToken);

    // TODO: initiate activation through email or admin

    return new ModelAndView(new RedirectView(getSuccessView()));
  }

  /**
   * Creates a new {@link com.googlecode.continuity.core.server.mvc.command.EditUserCommand} populated with a blank
   * {@link com.googlecode.continuity.core.client.User} object.
   *
   * @param request http request
   * @return fresh {@link com.googlecode.continuity.core.server.mvc.command.EditUserCommand}
   * @throws ServletException
   */
  protected EditUserCommand formBackingObject(HttpServletRequest request) throws ServletException {
    return new EditUserCommand(new User());
  }

  /**
   * Creates a new model that provides general data for displaying the registration form.
   *
   * @param request http request
   * @param command the current command object
   * @param errors errors that might have occured during form submission
   * @return model containing general data
   * @throws Exception
   */
  protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {
    ContinuityModel model = new ContinuityModel();

    if (request.getParameter("success") != null && request.getParameter("success").equals("1")) {
      model.put("completed", true);
      // TODO: make messaging formal, i.e. implement in model with lists and types. USE errors
      //model.put("successMessage", "Registration complete");
    }

    return model;
  }
}
